package game.resource.loader.builder;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.Attributes;

import common.Log;

import engine.filesystem.FileScanner;
import engine.filesystem.Resource_A;
import engine.video.blit.BlitMode_E;
import engine.video.blit.PixelBuffer;
import engine.video.blit.SpriteInfo;
import game.resource.SpriteResource;
import game.resource.SpriteSheetResource;
import game.resource.loader.PixelBufferLoader;
import game.resource.loader.tagparser.MaskTagParser;
import game.resource.loader.tagparser.RegularSheetTagParser;
import game.resource.loader.tagparser.TagParser_I;
import game.spritesheet.RegularSpriteSheet;
/**

<!-- Regular Sprite Sheet XML Specification
Tag		Attribute	Description
regularsheet			Indicates a regular grid of sprites on a sprite sheet.
					Note: Sub sprites on a regular sheet cannot be named.
		name			The name of this sprite sheet.
		image		Source image file
		rows			The number of rows on this sheet. (must be >= 1)
		cols			The number of columns on this sheet. (must be >= 1)
		maskimage*	If present, the indica
-->

<regularsheet name="sheet name" imagefile= "image.png" 
	maskfile="maskimage.png"  rows = "16" cols = "16" >
	
	Anything between tags will be echoed to the log.
	
	<mask color = 0x00/>
</regularsheet>

**/
public class RegularSpriteSheetBuilder implements ResourceBuilder_I {
	public RegularSpriteSheetBuilder() {
		MaskTag = new MaskTagParser();
		RegularSheetTag = new RegularSheetTagParser();
		_TagParsers = new TagParser_I[] { MaskTag, RegularSheetTag };
	}
	
	public MaskTagParser MaskTag;
	public RegularSheetTagParser RegularSheetTag;
	private TagParser_I[] _TagParsers;

	@Override
	public boolean pushTag(String tag, Attributes attributes) {
		for(TagParser_I parser : _TagParsers) {
			if(parser.parse(tag, attributes) == true) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public Resource_A getResource(FileScanner scanner) {
		if(scanner == null) {
			throw new NullPointerException("RegularSpriteSheetBuilder.getResource() : Argument cannot be null.");
		}
		
		String name = RegularSheetTag.Name;
		String imageFile = RegularSheetTag.ImageFile;
		String maskFile = RegularSheetTag.MaskFile;
		int rows = RegularSheetTag.Rows;
		int cols = RegularSheetTag.Cols;
		boolean generateMask = MaskTag.GenerateMask;
		byte maskColor = MaskTag.MaskColor;

		PixelBuffer image = null;
		PixelBuffer mask = null;
		
		// Check name.
		if(name == null) {
			Log.warning("Error loading sprite : name not set");
			return null;
		}
		
		if(imageFile == null) {
			Log.warning("Error loading sprite : imagefile not set");
			return null;
		}
		image = PixelBufferLoader.load(imageFile, scanner);
		// Load Sprite Image
		if(image == null) {
			Log.warning("Error loading sprite : image data not loaded");
			return null;
		}
		// Load Mask Image
		if(maskFile != null) {
			mask = PixelBufferLoader.load(maskFile, scanner);
		} else if (generateMask == true) { // or generate the mask.
			mask = image.generateMaskBuffer(maskColor);
		}
		
		SpriteInfo spriteInfo = new SpriteInfo(image, mask, null, null);
		RegularSpriteSheet sheet = new RegularSpriteSheet(spriteInfo, rows, cols);
		return new SpriteSheetResource(name, sheet);
	}
}
